package com.mdata.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mdata.common.RSAEncrypt;
import com.tencent.wework.Finance;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Decoder;

import java.io.IOException;

@Slf4j
@Component
public class FinanceUtil {


    public long getFinanceSdk(){
        long sdk = Finance.NewSdk();
        long ret = Finance.Init(sdk, "wwde557666af8f5216", "m2Z-JqvfzykTFFnNFmN7s-IZwxuSfKxJsahm76SQ8gE");
        if(ret != 0){
            Finance.DestroySdk(sdk);
            log.error("init sdk err ret " + ret);
        }
        return sdk;
    }

    public JSONArray getChatDataArray(){
        JSONArray jsonArray = new JSONArray();

        long sdk = getFinanceSdk();

        JSONObject chatDataResultObj = getChatData(sdk, 0, 1000);
        if(chatDataResultObj.getInteger("errcode") == 0) {
            JSONArray chatDataArray = chatDataResultObj.getJSONArray("chatdata");
            for (int i = 0; i < chatDataArray.size(); i++) {
                JSONObject chatDataObj = chatDataArray.getJSONObject(i);
                String encryptRandomKey = chatDataObj.getString("encrypt_random_key");
                String encryptChatMsg = chatDataObj.getString("encrypt_chat_msg");

                JSONObject chatDataJson = decryptData(sdk, encryptRandomKey, encryptChatMsg);
                jsonArray.add(chatDataJson);
            }
        }

        Finance.DestroySdk(sdk);
        return jsonArray;
    }

    public JSONObject getChatData(long sdk, int seq, int limit){
        //每次使用GetChatData拉取存档前需要调用NewSlice获取一个slice，在使用完slice中数据后，还需要调用FreeSlice释放。
        long slice = Finance.NewSlice();
        long ret = Finance.GetChatData(sdk, seq, limit, null, null, 60, slice);
        if (ret != 0) {
            Finance.FreeSlice(slice);
            return null;
        }
        String result = Finance.GetContentFromSlice(slice);
        Finance.FreeSlice(slice);
        return JSONObject.parseObject(result);
    }

    @SneakyThrows
    public JSONObject decryptData(long sdk, String encryptKey, String encryptChatMsg){
        String priKey =
                "-----BEGIN RSA PRIVATE KEY-----\n" +
                        "MIICWwIBAAKBgQCDtOom/+oDAYQJa7PRgDMrFwNjjdA/uPTv17Smq260EMWd1diH\n" +
                        "2eF+9nCRdNbpCDx14B9V994f7MOQNXGpA5KkCqlcoa0Vjab2P2WkODruLAhUyEQq\n" +
                        "QIhpXsoBXAk3eN08sl/N6S7pmEtFkQxJpxAmJkxK84AqXzqg0CaytaYTewIDAQAB\n" +
                        "AoGAAQ9iORkjOY34EZlX065b+xNO2CzKidDDy/BpCFRl6jYeWRRhD0reKUbzhgAy\n" +
                        "QKLr1akNp4P8ujYOJul/NjSMqBlexGfhbSt5vCeUEXudWk39o2dfVmtrjANUo286\n" +
                        "hpzYo9mSor5xGiwS3tigo8z9/M0XkjF+IavJpci4LEQkuxUCQQCeQIta7AJg97DY\n" +
                        "0vcXgUeRux6tMY6zSRmV2Z0qe2NFCqVnYZPPfXey/yVxY195iUInxF8bB9R+2dPT\n" +
                        "AvR1jhIvAkEA1Q7mzQX21OPdJmd0QgxvWoLjmSK5RexbP8QZhUn2eo4RPp/sadf4\n" +
                        "uddyoIQQ8Dz7QBwLXBLTnEfFxhP6Rmd8dQJAcuw6h0/HNnYJxpwF9trah5lU4L+R\n" +
                        "et+GJg1/lj8+yv+9OINjHRZnm+SwY1SgHBmyokB4NKAOEATOUHnf35LJ3wJASttL\n" +
                        "eClqSe4tWzrp+/LGhPSL5tfuZITgoozRsqUD9pdHs8sYky+QcisiWQdNtDqYxDOW\n" +
                        "QilTNAt7j1ahjm75qQJAae/6p15vGRjBQn8LtTX9c3kVYt9zoDQRjeGF+oQMrun8\n" +
                        "s6pE9NyAvUyxcMP4Yehe0B0WFaqwQ5A7n4Do2oFwHA==\n" +
                        "-----END RSA PRIVATE KEY-----\n";
        long msg = Finance.NewSlice();
        long ret = Finance.DecryptData(sdk, RSAEncrypt.decryptRSA(encryptKey,priKey), encryptChatMsg, msg);
        if (ret != 0) {
            Finance.FreeSlice(msg);
            return null;
        }
        String oneDataString = Finance.GetContentFromSlice(msg);
        Finance.FreeSlice(msg);
        return JSONObject.parseObject(oneDataString);
    }
}
